package clientmain.restful;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public final class Client {

    public static void main(String[] args) {
//    	get("http://localhost:9999/restHelloWorld/say/kk");
    	post("http://localhost:9999/restHelloWorld/getCatsByUser");
//    	post("http://localhost:9999/restHelloWorld/say");
    }


    public static void get(String targetURL){
		try {
	        URL restServiceURL = new URL(targetURL);
	        HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
	        httpConnection.setRequestMethod("GET");
	        httpConnection.setRequestProperty("Accept", "text/plain");
	        
	        if (httpConnection.getResponseCode() != 200) {
	               throw new RuntimeException("HTTP GET Request Failed with Error code : "
	                             + httpConnection.getResponseCode());
	        }
	        BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
	               (httpConnection.getInputStream())));
	        String output;
	        System.out.println("Output from Server:  \n");
	        while ((output = responseBuffer.readLine()) != null) {
	               System.out.println(output);
	        }
	        httpConnection.disconnect();
	   } catch (MalformedURLException e) {
	        e.printStackTrace();
	   } catch (IOException e) {
	        e.printStackTrace();
	   }
    }
    
    public static void post(String targetURL){
    	try {

            URL targetUrl = new URL(targetURL);

            HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
//            httpConnection.setRequestProperty("Content-Type", "text/plain");
            httpConnection.setRequestProperty("Content-Type", "application/xml");

//            String input = "name=kk";
            String input = "{\"name\":\"孙悟空\",\"adress\":\"花果山\"}";

            OutputStream outputStream = httpConnection.getOutputStream();
            outputStream.write(input.getBytes());
            outputStream.flush();

            if (httpConnection.getResponseCode() != 200) {
                   throw new RuntimeException("Failed : HTTP error code : "
                          + httpConnection.getResponseCode());
            }

            BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                          (httpConnection.getInputStream())));

            String output;
            System.out.println("Output from Server:\n");
            while ((output = responseBuffer.readLine()) != null) {
                   System.out.println(output);
            }

            httpConnection.disconnect();

       } catch (MalformedURLException e) {

            e.printStackTrace();

       } catch (IOException e) {

            e.printStackTrace();

      }
    }
}

